package org.itboys.pay.weixin;

import org.apache.commons.lang3.StringUtils;
import org.itboys.commons.utils.encryption.Digests;
import org.itboys.commons.utils.random.RandomUtils;


/**
 * 作者：江雄
 * 时间：2017年12月18日
 * 描述：微信退款实体类
 */
public class Refundorder {
	public static final String URL = "https://api.mch.weixin.qq.com/secapi/pay/refund";// 微信退款的链接
	private String appid;// 微信appid
	private String mchId;// 商户号
	private String nonceStr;//随机字符串。
	private String sign;// 签名
	private String signType="MD5";//签名方式
	private String transactionId;//交易订单号。二选一，建议使用这个
	private String outTradeNo;//内部订单号。二选一
	private String outRefundNo;//内部退款单号。
	private Integer totalFee;//订单总金额。只能为整数,单位为分
	private Integer refundFee;//退款金额。只能为整数,单位为分

	private String key;//商户KEY

	public Refundorder() {
	}

	public Refundorder(String appid, String mchId,
                       String transactionId,
                       String outRefundNo, Integer totalFee, Integer refundFee, String key) {
		this.appid = appid;
		this.mchId = mchId;
		this.nonceStr = RandomUtils.getClearRandomStr(32);;
		this.signType = "MD5";
		this.transactionId = transactionId;
		this.outRefundNo = outRefundNo;
		this.totalFee = totalFee;
		this.refundFee=refundFee;
		this.key=key;
	}



	public String getAppid() {
		return appid;
	}

	public void setAppid(String appid) {
		this.appid = appid;
	}

	public String getMchId() {
		return mchId;
	}

	public void setMchId(String mchId) {
		this.mchId = mchId;
	}

	public String getNonceStr() {
		return nonceStr;
	}

	public void setNonceStr(String nonceStr) {
		this.nonceStr = nonceStr;
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}

	public String getSignType() {
		return signType;
	}

	public void setSignType(String signType) {
		this.signType = signType;
	}

	public String getTransactionId() {
		return transactionId;
	}

	public void setTransactionId(String transactionId) {
		this.transactionId = transactionId;
	}

	public String getOutTradeNo() {
		return outTradeNo;
	}

	public void setOutTradeNo(String outTradeNo) {
		this.outTradeNo = outTradeNo;
	}

	public String getOutRefundNo() {
		return outRefundNo;
	}

	public void setOutRefundNo(String outRefundNo) {
		this.outRefundNo = outRefundNo;
	}

	public Integer getTotalFee() {
		return totalFee;
	}

	public void setTotalFee(Integer totalFee) {
		this.totalFee = totalFee;
	}

	public Integer getRefundFee() {
		return refundFee;
	}

	public void setRefundFee(Integer refundFee) {
		this.refundFee = refundFee;
	}



	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	/**
	 * 完整的xml格式的参数 含签名
	 * 
	 * @return
	 */
	public String getXML() {
		String signStr = "appid=" + this.appid;
		String xml = "<xml>" + "<appid>" + this.appid + "</appid>";

		xml += "<mch_id>" + this.mchId + "</mch_id>";
		signStr += "&mch_id=" + this.mchId ;

		xml +=  "<nonce_str>"+this.nonceStr + "</nonce_str>";
		signStr += "&nonce_str=" + this.nonceStr;

		xml +=  "<out_refund_no>"+this.outRefundNo + "</out_refund_no>";
		signStr += "&out_refund_no=" + this.outRefundNo;

		if (StringUtils.isNotBlank(this.outTradeNo)) {
			xml +=  "<out_trade_no>"+this.outTradeNo + "</out_trade_no>";
			signStr += "&out_trade_no=" + this.outTradeNo;
		}
		xml +=  "<refund_fee>"+this.refundFee + "</refund_fee>";
		signStr += "&refund_fee=" + this.refundFee;

		xml +=  "<total_fee>"+this.totalFee + "</total_fee>";
		signStr += "&total_fee=" + this.totalFee;
		if (StringUtils.isNotBlank(this.transactionId)) {
			xml +=  "<transaction_id>"+this.transactionId + "</transaction_id>";
			signStr += "&transaction_id=" + this.transactionId;
		}
		//2018年2月8日，加上退款接口里面没有说明的key
		signStr+="&key="+key;
		System.out.println("--------微信退款签名字符串------------"+signStr);
		//xml+="</xml>";
		signStr = Digests.md5(signStr).toUpperCase();

		this.sign = signStr;
		xml += "<sign>" + signStr + "</sign>" + "</xml>";
		return xml;
	}

}
